package com.oa.modules.approvalsec1.controller;
import java.util.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.oa.modules.approvalsec1.entity.ProcessEntity;
import com.oa.modules.approvalsec1.entity.ProcessItemEntity;
import com.oa.modules.approvalsec1.entity.ProjectScheduleEntity;
import com.oa.modules.approvalsec1.service.ProcessItemService;
import com.oa.modules.approvalsec1.service.ProcessService;
import com.oa.modules.approvalsec1.service.ProjectScheduleService;
import com.oa.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.oa.common.utils.PageUtils;
import com.oa.common.utils.R;
/**
 * 项目流程表
 *
 * @author jiangwei
 * @email 785297226@qq.com
 * @date 2021-12-10 09:15:39
 */
@RestController
@RequestMapping("process")
@Api(tags = "项目流程表相关接口")
public class ProcessController extends AbstractController {
    @Autowired
    private ProcessService processService;
    @Autowired
    private ProcessItemService processItemService;
    @Autowired
    protected ProjectScheduleService projectScheduleService;
    @Autowired
    ProjectScheduleController projectScheduleController;
    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "列表")
//    @RequiresPermissions("generator:process:list")
    public R list(@RequestParam Map<String, Object> params){
            PageUtils page = processService.queryPage(params);
        return R.ok().put("page", page);
    }



    @GetMapping("/info/{id}")
    @ApiOperation(value = "信息")
    public R info(@PathVariable("id") Integer id){
		ProcessEntity process = processService.getById(id);
		//修改流程信息的数据回显
        LambdaQueryWrapper<ProcessItemEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProcessItemEntity::getProcessId,id).eq(ProcessItemEntity::getIsEnabled,1);
        List<ProcessItemEntity> itemEntityList = processItemService.list(lambdaQueryWrapper);
        System.out.println(itemEntityList.size());
        List<Integer> scheduleList = new ArrayList<>();
        for (ProcessItemEntity processItemEntity : itemEntityList) {
            LambdaQueryWrapper<ProjectScheduleEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ProjectScheduleEntity::getId,processItemEntity.getScheduleId());
            ProjectScheduleEntity entity = projectScheduleService.getOne(wrapper);
            List<ProjectScheduleEntity> childList = projectScheduleService.list(new QueryWrapper<ProjectScheduleEntity>().eq("parent_id", entity.getId()));
            if (entity.getParentId()>0&&(childList==null || childList.size()==0)) {
                scheduleList.add(processItemEntity.getScheduleId());
            }
        }
        return R.ok().put("process", process).put("scheduList",scheduleList);
    }

    /**
     * 保存@RequestParam(value = "itemList[]")
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存")
    public R save(@RequestBody ProcessEntity process){
        process.setCreateDate(new Date());
        process.setStatus(0);
        process.setCreateOper("1");
        processService.saveProcess(process,process.getItemList());
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public R update(@RequestBody ProcessEntity process){
        process.setUpdateDate(new Date());
//        process.setUpdateOper(getUserId().toString());
        processService.updateById(process);
        //将流程下的所有进度改为未启用
        processItemService.update(new UpdateWrapper<ProcessItemEntity>().eq("process_id",process.getId()).set("is_enabled",0));
        //再将修改后的镀改为启用
        Integer[] itemList = process.getItemList();
        if(itemList.length>0){
            for (int i = 0; i < itemList.length; i++) {
                processItemService.update(new UpdateWrapper<ProcessItemEntity>().eq("process_id",process.getId()).eq("schedule_id",itemList[i]).set("is_enabled",1));
            }
        }

        //获取修改前最后一个排序号
        ProcessItemEntity entity = processItemService.getOne(new QueryWrapper<ProcessItemEntity>().eq("process_id", process.getId()).eq("is_enabled", 1).orderByDesc("sort").last("limit 1"));
        System.out.println(entity.getSort());
        int count=entity.getSort()+1;
        //获取新增的进度
        List<ProcessItemEntity> processList = processItemService.list(new QueryWrapper<ProcessItemEntity>().eq("process_id", process.getId()).eq("is_enabled", 1).isNull("sort"));
        //将新增的流程从count开始排序
        for (ProcessItemEntity item : processList) {
            item.setSort(count);
            processItemService.updateById(item);
            count++;
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    public R delete(@RequestBody Integer[] ids){
		processService.removeByIds(Arrays.asList(ids));
        LambdaQueryWrapper<ProcessItemEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        for (Integer id : ids) {
            lambdaQueryWrapper.eq(ProcessItemEntity::getProcessId,id);
            processItemService.remove(lambdaQueryWrapper);
        }

        return R.ok();
    }

}
